package com.heima;

import com.heima.Dao.UserDao;
import com.heima.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Text {
    @Test
    public void findAll() throws IOException {
        //获取核心配置文件，得到输入流对象
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        //构造会话工厂建造类
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //通过建造类得到会话工厂
        SqlSessionFactory sessionFactory = builder.build(inputStream);
        //通过会话工厂得到会话对象
        SqlSession session = sessionFactory.openSession();
        //通过会话对象得到usermapper接口的代理对象
        UserDao userDao = session.getMapper(UserDao.class);
        //执行查询操作
        List<User> list = userDao.findAll();
        //遍历内容
        for (User user : list) {
            System.out.println(user);
        }
        //关闭流
        session.close();
    }
}
